﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverAmp.Common;
using SilverAmp.Common.DecoupledEvents;

namespace SilverAmp.Extensions.PlayListExtension
{
    [Export]
    public class PlayListExtensionViewModel : IPartImportsSatisfiedNotification, INotifyPropertyChanged
    {
        [Import]
        public MainModel Model { get; set; }

        [Import]
        public PlayListExtensionView View { get; set; }

        private int _selectedIndex=-1;
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value; }
        }

        public ICommand SelectSongCommand { get; set; }
        public ICommand LookupSongCommand { get; set; }

        public PlayListExtensionViewModel()
        {
            
            SelectSongCommand = new DelegateCommand<TrackInfo>(OnSelectSongCommand);
            LookupSongCommand = new DelegateCommand<TrackInfo>(OnLookupSongCommand);
        }

        private void OnLookupSongCommand(TrackInfo trackInfo)
        {
            if(trackInfo!=null)
                DecoupledEventAggregator.GetInstance().GetEvent<EditSongEvent>().Publish(trackInfo);
        }

        private void OnSelectSongCommand(TrackInfo trackInfo)
        {
            DecoupledEventAggregator.GetInstance().GetEvent<SelectSongEvent>().Publish(trackInfo);
        }

        public void OnImportsSatisfied()
        {
            View.DataContext = this;
        }

        private bool firstTime = true;
        public void Initialize()
        {
            if (firstTime)
            {
                Model.LoadID3SongInformation();
                firstTime = false;
            }

            SelectedIndex = -1;
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedIndex"));
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}
